//
//  LoginViewController.m
//  Remind
//
//  Created by macZF on 2017/10/26.
//  Copyright © 2017年 2017.09.27. All rights reserved.
//

#import "LoginViewController.h"
#import "RgisterViewController.h"
#import "MainViewController.h"
#import "DataCach.h"
#import "LGSideMenuController.h"
#import "UIViewController+LGSideMenuController.h"
#import "AppDelegate.h"

@interface LoginViewController () <UITextFieldDelegate>

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationItem setHidesBackButton:YES];
    self.title = @"登录";
    _userBGView.layer.cornerRadius = 10.0;
    _passwordBGView.layer.cornerRadius = 10.0;
    _userTextField.tag = 8000;
    [DataCach addTextFieldInputAccessoryView:_userTextField];
    [DataCach addTextFieldInputAccessoryView:_passwordTextField];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if(textField.tag == 8000){
        [textField endEditing:YES];
        [_passwordTextField becomeFirstResponder];
        return YES;
    }
    [textField endEditing:YES];
    return YES;
}

- (IBAction)longinButtonOnClick:(id)sender {
    NSMutableDictionary *users = [[NSUserDefaults standardUserDefaults] objectForKey:@"users"];
    NSLog(@"用户列表%@",users);
    if(!users) users = [NSMutableDictionary new];
    NSString *password = [users objectForKey:_userTextField.text];
    if(!password) password = @"";
    NSString *message = @"登录成功";
    
    if(![password isEqualToString:_passwordTextField.text]) message = @"密码错误";
    if([_userTextField.text isEqualToString:@""]||[_passwordTextField.text isEqualToString:@""]) message = @"请输入账号/密码";
    
    UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if([message isEqualToString:@"登录成功"]){
            [DataCach setUserInformation:_userTextField.text];
            [self.navigationController popViewControllerAnimated:YES];
            MainViewController *mainCon = [[MainViewController alloc] init];
            [DataCach selectSideMenuPushViewController:mainCon Index:0];
        }
    }];
    [alertCon addAction:action];
    [self.navigationController presentViewController:alertCon animated:YES completion:nil];
}

- (IBAction)registeredButtonOnClick:(id)sender {
    RgisterViewController *registerCon = [self.storyboard instantiateViewControllerWithIdentifier:@"register"];
    [self.navigationController pushViewController:registerCon animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
